﻿using CommonDevelop;
using CommonEngine;
using CommonFP;
using ManageDto;
using ManageService;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using WebAdmin.Handler;

namespace WebAdmin.Areas.Org.Controllers
{
    /// <summary>
    /// 部门管理
    /// </summary>
    [Area("Org")]
    [Route("Org/[controller]/[action]")]
    public class DeptController : BaseController
    {
        private readonly IDeptService _DeptService;
        /// <summary>
        /// 构造注入
        /// </summary>
        public DeptController(IDeptService DeptService)
        {
            this._DeptService = DeptService;
        }

        /// <summary>
        /// 视图列表
        /// </summary>
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Title = "部门管理";
            return View();
        }

        /// <summary>
        /// 选择上级
        /// </summary>
        [HttpGet]
        public ActionResult GetSelectLayer(string id)
        {
            ViewBag.Title = "上级部门";
            ViewBag.ComID = id;
            return View();
        }

        #region--Select--

        /// <summary>
        /// 输出绑定树组件的数据
        /// </summary>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetTreeData()
        {
            List<CompanyDto> data = this._DeptService.GetCompany().ToList();

            var Data = BizPage.GetCompanyTree(data);
            //这种写法针对jstree按Url取数据
            return this.ToJsonContentNoRN(Data);
        }

        /// <summary>
        /// 列表数据
        /// </summary>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetTreeList(string companyId, string keyword)
        {
            List<DeptDto> data = null;
            if (companyId.IsEmpty())
                data = new List<DeptDto>();
            else
                data = this._DeptService.GetDeptByCompanyId(companyId, keyword).ToList();

            return this.SuccessJson(data);
        }

        #endregion

        #region--增删改--

        [HttpPost]
        [AjaxOnly]
        public ActionResult NodeAdd(string id)
        {
            var com = this._DeptService.GetCompany(id);
            if (com == null)
                return this.Fail("Null Company");

            DeptDto en = new DeptDto();
            en.DeptCode = this._DeptService.BuildSeq();
            en.CompanyId = com.CompanyId;
            en.CompanyName = com.FullName;
            return this.SuccessJson(en);
        }

        [HttpPost]
        [AjaxOnly]
        public ActionResult NodeDelete(string keyValue)
        {
            Validation<bool> vb = this._DeptService.Delete(keyValue);
            var ValTuple = vb.Outcome(a => a);
            if (ValTuple.Errors.IsEmpty())
                return ValTuple.result ? this.Success("操作成功") : this.Fail("操作失败");
            else
                return this.Fail(ValTuple.Errors);
        }

        [HttpPost]
        [AjaxOnly]
        public ActionResult NodeEdit(string keyValue)
        {
            var outcome = this._DeptService.Get<DeptDto>(keyValue);
            return outcome.Match(() => this.Fail("Null Error"),
                en =>
                {
                    ComDeptDto box = this._DeptService.GetDeptItem(en.ParentId);
                    if (box == null)
                        box = this._DeptService.GetDeptItem(en.CompanyId);

                    en.ParentName = box?.DeptName;
                    en.CompanyName = box?.ComName;
                    return this.SuccessJson(en);
                });
        }

        [HttpPost]
        [AjaxOnly]
        [DisableRequestSizeLimit]
        public ActionResult NodeSave([FromForm]string DataText)
        {
            DeptDto em = PageUtility.FormMap<DeptDto>(DataText);
            if (em == null)
                return this.Fail("表单解读失败");
            if (em.FullName.IsEmpty())
                return this.Fail("部门名称不能为空");

            Validation<bool> vb = FP.Error("未执行");
            if (em.ModifyUserId.IsEmpty())
                vb = this._DeptService.Add(em);
            else
                vb = this._DeptService.Save(em);

            var ValTuple = vb.Outcome(a => a);
            if (ValTuple.Errors.IsEmpty())
                return ValTuple.result ? this.Success("操作成功") : this.Fail("操作失败");
            else
                return this.Fail(ValTuple.Errors);
        }

        #endregion

    }
}